package com.ll.studentservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ll.commonUtils.JwtUtils;
import com.ll.commonUtils.MD5;
import com.ll.servicebase.exceptionhandler.LLException;
import com.ll.studentservice.entity.EduTeacher;
import com.ll.studentservice.entity.vo.LoginVo;
import com.ll.studentservice.entity.vo.RegisterVo;
import com.ll.studentservice.mapper.EduTeacherMapper;
import com.ll.studentservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 教师 服务实现类
 * </p>
 *
 * @author ll
 * @since 2020-11-30
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //教师注册
    @Override
    public boolean register(RegisterVo registerVo) {
        //获取注册的数据
        String phone = registerVo.getPhone();
        String name = registerVo.getName();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //判断注册字段是否为空
        if (StringUtils.isEmpty(phone)){
            throw new LLException(20001, "注册手机号为空");
        }

        if (StringUtils.isEmpty(name)){
            throw new LLException(20001, "用户名为空");
        }

        if (StringUtils.isEmpty(password)){
            throw new LLException(20001, "密码为空");
        }

        if (StringUtils.isEmpty(code)){
            throw new LLException(2001, "验证码为空");
        }

        System.out.println("---------------------------------------");
        System.out.println("教师注册用户名: " + name);
        System.out.println("教师注册手机号: " + phone);
        System.out.println("教师注册密码: " + password);

        // 判断验证码是否正确
        // 从redis中获取验证码
        String redisCode = redisTemplate.opsForValue().get(phone);
        System.out.println("Redis中存储的key和验证码: " + phone + ":" + redisCode);

        //判断用户输入的和redis中存储的验证码是否一致
        if (!code.equals(redisCode)){
            throw new LLException(20001, "验证码错误!");
        }

        //判断手机号是否为空
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new LLException(20001, "该手机号已注册");
        }

        //添加到数据库中
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setName(name);
        eduTeacher.setPhone(phone);
        eduTeacher.setPassword(MD5.encrypt(password));
        eduTeacher.setIsDeleted(false);
        eduTeacher.setAvatar("https://edu-526.oss-cn-shenzhen.aliyuncs.com/2020/10/28/8753696b0e484f4095aa2b1bb2641e74file.png");
        int flag = baseMapper.insert(eduTeacher);

        return flag > 0 ? true : false;
    }

    @Override
    public String login(LoginVo loginVo) {
        //获取登录的手机号和密码
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new LLException(20001, "手机号或密码为空,登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        EduTeacher eduTeacher = baseMapper.selectOne(wrapper);

        //判断查询出来的对象是否为空
        if (StringUtils.isEmpty(eduTeacher)){
            throw new LLException(20001, "数据库没有这个手机号(测试)");
        }

        System.out.println("传过来的手机号: " + phone);
        System.out.println("数据库的密码" + eduTeacher.getPassword());
        System.out.println("用户输入的密码MD5加密: " + MD5.encrypt(password));

        //判断密码
        //因为存储到数据库的密码肯定加密的
        //把我们输入的密码先进行加密,在和数据库的密码进行比较
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(eduTeacher.getPassword())){
            throw new LLException(20001, "手机号或者密码错误");
        }

        //判断用户是否被禁用

        //登录成功
        //生成token字符串,使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(eduTeacher.getId(), eduTeacher.getName());
        return jwtToken;
    }
}
